import {
	login,
	auto,
	logout,
	getInfo
} from '@/api/login'
import {
	getToken,
	setToken,
	setName,
	getName,
	setRoles,
	getRoles,
	getAvatar,
	setAvatar,
	removeToken
} from '@/utils/auth'

const user = {
	state: {
		token: getToken(),
		name: getName(),
		avatar: getAvatar(),
		roles: getRoles(),
		permissions: []
	},

	mutations: {
		SET_TOKEN: (state, token) => {
			state.token = token
		},
		SET_NAME: (state, name) => {
			state.name = name
			console.log(state);
		},
		SET_AVATAR: (state, avatar) => {
			state.avatar = avatar
		},
		SET_ROLES: (state, roles) => {
			state.roles = roles
		},
		SET_PERMISSIONS: (state, permissions) => {
			state.permissions = permissions
		}
	},

	actions: {
		// 登录
		Login({
			commit
		}, userInfo) {
			const Login_Code = userInfo.username.trim()
			const Login_Pwd = userInfo.password
			const code = ''
			const uuid = ''
			return new Promise((resolve, reject) => {
				login(Login_Code, Login_Pwd).then(res => {
					console.log(res)
					if (res.code == 1) {
						setToken(res.token);
						setName(res.data.name);
						setAvatar(res.data.userface);
						commit('SET_TOKEN', res.token)
						commit('SET_NAME', res.data.name)
						commit('SET_AVATAR', res.data.userface)
						userInfo.depid = res.data.depid;
						userInfo.companyid = res.data.companyid;
						let userinfo = JSON.stringify(userInfo);
						localStorage.setItem("userinfo", userinfo);
						auto(res.data.id).then(res => {
							console.log(res)
						
							if (res.code == 1) {
								let roles = [];
								res.obj.forEach(item=>{
									if(item.select){
										roles.push(item.id);
									}
								})
								setRoles(roles);
								commit('SET_ROLES', roles)
							} 
						}).catch(error => {
							reject(error)
						})
						resolve()
					} else {
						 reject()	
					}

				}).catch(error => {
					reject(error)
				})
			})
		},

		// 获取用户信息
		GetInfo({
			commit,
			state
		}) {
			return new Promise((resolve, reject) => {
				getInfo(state.token).then(res => {
					const user = res.data;
					console.log(user)
					const avatar = user.avatar == "" ? require("@/assets/images/profile.jpg") : user
						.avatar;
					commit('SET_PERMISSIONS', user.permissions)
					commit('SET_NAME', user.userName)
					commit('SET_AVATAR', avatar)
					resolve(res)
				}).catch(error => {
					reject(error)
				})
			})
		},
		//获取权限
		GetPermissions({
			commit
		}) {
			return new Promise((resolve, reject) => {
				getInfo().then(result => {
					console.log(result)
					commit('SET_PERMISSIONS', result.data.permissions)
					commit('SET_NAME', result.data.userName)
					commit('SET_AVATAR', result.data.avatar)
				})

				resolve();
			})
		},
		// 退出系统
		LogOut({
			commit,
			state
		}) {
			return new Promise((resolve, reject) => {
				commit('SET_TOKEN', '')
				commit('SET_ROLES', [])
				commit('SET_PERMISSIONS', [])
				removeToken()
				resolve()
			})
		},

		// 前端 登出
		FedLogOut({
			commit
		}) {
			return new Promise(resolve => {
				commit('SET_TOKEN', '')
				removeToken()
				resolve()
			})
		}
	}
}

export default user
